const api = require("../../../utils/cloudAPIUtils");
const app = getApp();

Page({
    data: {
        couponList:[],
        modalName:'',
        couponInfo:{},
    },
    hideModal(){
        this.setData({modalName:''})
    },
    btnGetCoupon(){
        const self = this;
        api.post(api.url.couponGet,{
            activityId:this.data.couponInfo.id,
            placeId:''
        },res=>{
            this.setData({modalName:''});
            if(res.data.code==200){
                wx.showToast({title:'领取成功！'})
                self._getInfo();
            }else{
                wx.showModal({
                    title: '领取优惠券',
                    content: res.data.msg,
                    showCancel:false
                })
            }
        })
    },
    btnScan(){
        const self = this;
        wx.scanCode({
            success(res){
                const qrcode = res.result.split('/');
                if(qrcode[4]=="coupon_activity"&&parseInt(qrcode[5])>0){
                    //优惠券二维码
                    api.post(api.url.couponInfo,{
                        activityId:qrcode[5]
                    },res=>{
                        console.log(res.data.body)
                        if(res.data.code==200){
                            self.setData({
                                couponInfo:res.data.body,
                                modalName:'coupon'
                            })
                        }else{
                            wx.showModal({
                            title: '温馨提示',
                            content: res.data.msg||'优惠券异常！',
                            showCancel:false,
                            })
                        }
                    })
                }else{
                    wx.showModal({
                    title: '温馨提示',
                    content: '请扫正确的二维码',
                    showCancel:false,
                    })
                }
            }
        })
    },
    onLoad(options) {
        let info = app.globalData.userInfo;
        console.log(info)
        if(info.loginToken){
          if(info.memberName =='匿名用户'){
              wx.navigateTo({url: '/pages/mine/info/info?show=true'})
          }
        }else{
            wx.showModal({
              title: '登录失败',
              content: '请联系客服人员！',
              showCancel:false
            })
        }
        this._getInfo();
    },
    _getInfo(){
        api.post(api.url.couponList,{},res=>{
            console.log(res)
            if(res.data.code==200){
                this.setData({
                    couponList:res.data.body.items
                })
            }
        })
    }
})